package com.ylx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.model.common.dtos.ResponseResult;
import com.ylx.model.common.enums.AppHttpCodeEnum;
import com.ylx.model.user.dtos.LoginDto;

import com.ylx.model.user.pojo.PxxAdmin;
import com.ylx.service.PxxAdminService;
import com.ylx.mapper.PxxAdminMapper;
import com.ylx.utils.common.AppJwtUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
* @author 许文倩
* @description 针对表【pxx_user】的数据库操作Service实现
* @createDate 2024-06-24 18:40:50
*/
@Service
public class PxxAdminServiceImpl extends ServiceImpl<PxxAdminMapper, PxxAdmin>
    implements PxxAdminService {

    @Resource
    private PxxAdminMapper pxxUserMapper;

    /**
     * 登录
     * @param loginDto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto loginDto) {
        // 正常登录 账号密码不为空
        if (StringUtils.isNotEmpty(loginDto.getPhone())&&StringUtils.isNotEmpty(loginDto.getPassword())){
            LambdaQueryWrapper<PxxAdmin> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(PxxAdmin::getPhone,loginDto.getPhone());
            PxxAdmin pxxUser = pxxUserMapper.selectOne(wrapper);
            // 账号不存在
            if (pxxUser==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"账号不存在");
            }
            // 账号存在
            String salt = pxxUser.getSalt();
            String password = DigestUtils.md5DigestAsHex((loginDto.getPassword() + salt).getBytes());
            // 密码错误
            if (!password.equals(pxxUser.getPassword())){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"密码错误");
            }
            // 密码正确
            String token = AppJwtUtil.getToken(Long.valueOf(pxxUser.getId()));
            Map<String,Object> map=new HashMap<>();
            map.put("token", token);
            map.put("user", pxxUser);
            return ResponseResult.okResult(map);

        }else {
            // 账号密码为空  游客登录
            String token = AppJwtUtil.getToken(0L);
            Map<String,Object> map=new HashMap<>();
            map.put("token", token);
            return ResponseResult.okResult(map);
        }
    }

    /**
     * 注册
     * @param pxxUser
     * @return
     */
    @Override
    public ResponseResult regist(PxxAdmin pxxUser) {
        // 判断账号密码是否为空
        if (StringUtils.isBlank(pxxUser.getPassword()) || StringUtils.isBlank(pxxUser.getPhone())){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"账号或密码不能为空");
        }
        if(pxxUser.getRepassword().equals(pxxUser.getPassword())) {
            PxxAdmin pxxUser1 = pxxUserMapper.selectOne(new LambdaQueryWrapper<PxxAdmin>().eq(PxxAdmin::getPhone, pxxUser.getPhone()));
            // 验证账号不存在
            if (pxxUser1 == null) {

                // 不存在
                // 生成盐
                String salt = UUID.randomUUID().toString().replaceAll("-", "");
                pxxUser.setSalt(salt);
                // 密码加密
                String password = DigestUtils.md5DigestAsHex((pxxUser.getPassword() + salt).getBytes());
                pxxUser.setPassword(password);
                pxxUserMapper.insert(pxxUser);
                return ResponseResult.okResult(200, "注册成功");
            } else {
                // 账号存在
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该账号已存在");
            }
        }
        else {
                return ResponseResult.errorResult(402,"两次密码确认不一致");
            }
        }
}





